<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.magicTaskCode" placeholder="任务Code" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.magicTaskTypeEnum" placeholder="任务类型" clearable filterable>
          <el-option label="生成图片" value="WMH_GALAXY_MAGIC_TASK_IMAGE_GEN"></el-option>
          <el-option label="图片处理" value="WMH_GALAXY_MAGIC_TASK_IMAGE_PROCESS"></el-option>
          <el-option label="全量同步" value="WMH_GALAXY_MAGIC_TASK_ES_FULL_UPDATE"></el-option>
          <el-option label="订单处理" value="WMH_GALAXY_MAGIC_TASK_ORDER_PROCESS"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">创建任务</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
              style="width: 100%;">
      <el-table-column prop="magicTaskCode" header-align="center" align="center" label="任务Code">
      </el-table-column>
      <el-table-column prop="magicTaskTypeName" header-align="center" align="center" label="任务类型">
      </el-table-column>
      <el-table-column prop="magicTaskStatusName" header-align="center" align="center" label="任务状态">
      </el-table-column>
      <el-table-column prop="magicTaskEntityTypeName" header-align="center" align="center" label="主体类型">
      </el-table-column>
      <el-table-column prop="magicTaskEntityValue" header-align="center" align="center" label="主体值">
      </el-table-column>
      <el-table-column prop="magicTaskEventTypeName" header-align="center" align="center" label="事件类型">
      </el-table-column>
      <el-table-column prop="magicTaskEventValue" header-align="center" align="center" label="事件值">
        <template slot-scope="scope">
          <div style="max-height: 80px; overflow-y: auto;">
            {{ scope.row.magicTaskEventValue }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="executeTime" header-align="center" align="center" label="调度时间">
      </el-table-column>
      <el-table-column prop="runningTime" header-align="center" align="center" label="执行时间">
      </el-table-column>
      <el-table-column prop="retryTime" header-align="center" align="center" label="重试次数">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="executeWormholeGalaxyTask(scope.row.magicTaskCode)">执行任务</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.magicTaskCode)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
                   :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
                   layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './wmhGalaxyTask-add-or-update'

export default {
  data() {
    return {
      dataForm: {
        magicTaskCode: '',
        magicTaskTypeEnum:''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/galaxybase/magictask/queryWormholeGalaxyMagicTaskList'),
        method: 'post',
        data: this.$http.adornData({
          isPage: true,
          pageNumb: this.pageIndex-1,
          pageSize: this.pageSize,
          magicTaskCode: this.dataForm.magicTaskCode,
          magicTaskType:this.dataForm.magicTaskTypeEnum
        })
      }).then(({ data }) => {
        if (data && data.success) {
          this.dataList = data.data
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(magicTaskCode) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(magicTaskCode)
      })
    },

    executeWormholeGalaxyTask(magicTaskCode){
      this.$confirm(`是否执行${magicTaskCode}任务?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=> {
        this.$http({
          url: this.$http.adornUrl('/galaxybase/magictask/executeWormholeGalaxyMagicTask'),
          method: 'post',
          data: this.$http.adornData({
            magicTaskCode: magicTaskCode
          })
        }).then(({ data }) => {
          if (data && data.success) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,

              onClose: () => {}
            })
            this.getDataList()
          }else {
            this.$message.error(data.msg)
            this.getDataList()
          }
        })
      })

    },
    // 删除
    deleteHandle(magicTaskCode) {
      this.$confirm(`确定删除任务${magicTaskCode}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/galaxybase/magictask/deleteWormholeGalaxyMagicTask'),
          method: 'post',
          data: this.$http.adornData( {
            magicTaskCode:magicTaskCode
          })
        }).then(({ data }) => {
          if (data && data.success) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
